﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Caching;
using WinnerCMS.Common;
namespace WinnerCMS.CacheStorager
{
    /// <summary>
    /// 使用自带缓存
    /// </summary>
    public class RunTimeCacheStorager : ICacheStorager
    {

        /// <summary>
        /// 新增缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="Value"></param>
        /// <param name="datetime"></param>
        public void Add<T>(string key, T Value)
        {
            CacheHelper.Add<T>(key, Value, 60);
        }
        public void Add<T>(string key, T value, int datetime)
        {
            CacheHelper.Add<T>(key, value, DateTimeType.Second, datetime);
        }
        public void Add<T>(string key, T value, DateTimeType datetimetype, int datetime)
        {
            CacheHelper.Add<T>(key, value, datetimetype, datetime);
        }
        /// <summary>
        /// 获取缓存
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public string Get(string key)
        {
            return CacheHelper.Get<string>(key);
        }

        /// <summary> 
        /// 获取缓存 
        /// </summary> 
        /// <typeparam name="T"> </typeparam> 
        /// <param name="key"> </param> 
        /// <returns> </returns> 
        public T Get<T>(string key)
        {
            return CacheHelper.Get<T>(key);
        }
        /// <summary>
        /// 试图获取缓存
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="key"></param>
        /// <param name="value"></param>
        /// <returns></returns>
        public bool TryGet<T>(string key, out T value) where T : class
        {
            object obj = HttpRuntime.Cache.Get(key);
            if (obj == null)
            {
                value = default(T);
                return false;
            }
            else
            {
                value = (T)obj;
                return true;
            }
        }
        /// <summary> 
        /// 清空缓存 
        /// </summary> 
        public void FlushAll()
        {
            HttpRuntime.Close();
        }
        /// <summary>
        /// 到期时间
        /// </summary>
        public int TimeOut { set; get; }
        /// <summary> 
        /// 移除缓存 
        /// </summary> 
        /// <param name="key"> </param> 
        public void Remove(string key)
        {
            try
            {
                HttpRuntime.Cache.Remove(key);
            }
            catch { }
        }
        public void RemoveLike(string key)
        {
            try
            {
                List<string> names = new List<string>();
                var ienum = HttpRuntime.Cache.GetEnumerator();
                while (ienum.MoveNext())
                {
                    if (ienum.Key.ToString().StartsWith(key))
                    {
                        names.Add(ienum.Key.ToString());
                    }
                }
                foreach (var v in names)
                {
                    HttpRuntime.Cache.Remove(v);
                }
            }
            catch { }
        }


        public System.Collections.Hashtable Stats()
        {
            throw new NotImplementedException();
        }

        public void Shutdown()
        {
            throw new NotImplementedException();
        }
    }
}
